$(function () {
    Time();
    load();
})
let list = [];

function fh() {
    localStorage.removeItem("Mdesignprocedures");
    location.href = "../../li_meng/SouSuo.html";
}

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#datetime").val(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

//表单动态添加
function load() {
    $("#submit").prop("hidden", true)
    let u = localStorage.getItem("user");
    let user = JSON.parse(u);
    $("#name").val(user.rname);

    let Mdesignprocedures = JSON.parse(localStorage.getItem("Mdesignprocedures"));
    $("#productname").val(Mdesignprocedures.productname);
    $("#productid").val(Mdesignprocedures.productid);

    let str = " <tr >\n" +
        "           <th>点选</th>\n" +
        "           <th>工序名称</th>\n" +
        "           <th>工序编号</th>\n" +
        "           <th>描述</th>\n" +
        "           <th>工时数</th>\n" +
        "           <th>工时单位</th>\n" +
        "           <th>单位工时成本</th>\n" +
        "           <th>工时完成小计(元)</th>\n" +
        "       </tr>\n";

    console.log(list)
    if (list.length != 0) {
        for (let i = 0; i < list.length; i++) {
            str +=
                "  <tr>\n" +
                "      <td><input type=\"checkbox\" name='checkbox' value='" + list[i].id + "'></td>\n" +
                "      <td>" + list[i].dname + "</td>\n" +
                "      <td>" + list[i].did + "</td>\n" +
                "      <td>" + list[i].dmiaoshu + "</td>\n" +
                "      <td><input type=\"number\" value='1' class='GongShi' style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
                "      <td>h</td>\n" +
                "      <td><input type=\"number\"  value='1' class='ChengBen' style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
                "      <td><span class='xiaoji' ></span></td>\n" +
                "  </tr>";
        }
    }
    $("#tableAdd").html(str);
}

//动态删除
function Del() {
    let ids = [];
    $("[name='checkbox']:checked").each(function () {
        ids.push($(this).val());
    })
    let ls = [];
    if (ids.length > 1 || ids.length == 0) {
        return;
    } else {
        let js =0;
        for (let i = 0; i < ids.length; i++) {
            for (let j = 0; j < list.length; j++) {
                if (ids[i] != list[j].id) {
                    ls[js] = list[j];
                    js +=1;
                }
            }
        }
        list = ls;
        load();
    }
}

function add() {
    $("#AccRoleModal").modal("show");
    page();
}

function page() {
    let ls = [{id: 1, did: "01", dname: "组装", dmiaoshu: "先组装"},
        {id: 2, did: "02", dname: "测试", dmiaoshu: "组装完再测试"},
        {id: 3, did: "03", dname: "包装", dmiaoshu: "测试完就包装"}];
    let str = "    <tr>\n" +
        "            <th>工序编号</th>\n" +
        "            <th>工序名称</th>\n" +
        "            <th>工序描述</th>\n" +
        "            <th>操作</th>\n" +
        "        </tr>\n"
    for (let i = 0; i < ls.length; i++) {
        let s = JSON.stringify(ls[i]);
        str += "  <tr>\n" +
            "      <td>" + ls[i].did + "</td>\n" +
            "      <td>" + ls[i].dname + "</td>\n" +
            "      <td>" + ls[i].dmiaoshu + "</td>\n" +
            "      <td><button class='btn btn-primary' onclick='maAdd(" + s + ")'>添加</button></td>\n" +
            "  </tr>";
    }
    $("#tm01").html(str);
}

function maAdd(ma) {
    console.log(ma)
    if (list.length < 1) {
        list.push(ma);
        load();
    } else {
        for (let i = 0; i < list.length; i++) {
            if (list[i].id == ma.id) {
                return;
            }
        }
        list[list.length] = ma;
        load();
    }
}

//预览
function yl() {
    if (list.length < 1) {
        $("#tishi").text("请选择工序 ！")
        $("#tishi").css("color", "black")
        $("#staticBackdrop").modal("show");
        return;
    }
    $("#submit").removeAttr("hidden");
    $("#XQ").prop("hidden", true);
    $(".ad").prop("hidden", true);

    let shu = [];
    $(".GongShi").each(function () {
        shu.push($(this).val());
    })
    let dj = [];
    $(".ChengBen").each(function () {
        dj.push($(this).val());
    })
    let s = 0;
    $(".xiaoji").each(function () {
        console.log(shu[s] * dj[s])
        $(this).text(parseInt(shu[s]) * parseInt(dj[s]))
        s = s + 1;
    })
}

//提交数据
function sut() {
    let shu = [];
    $(".GongShi").each(function () {
        shu.push($(this).val());
    })

    let xiaoji = 0;
    $(".xiaoji").each(function () {
        xiaoji += parseInt($(this).text());
    })

    for (let i = 0; i < shu.length; i++) {
        if (shu[i] == "") {
            $("#tishi").text("数量不能为小于等于0 ！")
            $("#tishi").css("color", "black")
            $("#staticBackdrop").modal("show");
            return;
        }
    }
    let dj = [];
    let cb = 0;
    $(".ChengBen").each(function () {
        dj.push($(this).val());
        cb += $(this).val();
    })
    for (let i = 0; i < dj.length; i++) {
        if (parseInt(dj[i]) <= 0) {
            alert("工时不能为小于等于0！")
            return;
        }
    }

    let Mdesignprocedures = JSON.parse(localStorage.getItem("Mdesignprocedures"));
        let Mdesignprocedure = {
            //产品i级分类编号
            "firstkindid": Mdesignprocedures.firstkindid,
            //产品i级分类名称
            "firstkindname": Mdesignprocedures.firstkindname,
            //产品ii级分类编号
            "secondkindid": Mdesignprocedures.secondkindid,
            //产品ii级分类名称
            "secondkindname": Mdesignprocedures.secondkindname,
            //产品iii级分类编号
            "thirdkindid": Mdesignprocedures.secondkindname,
            //产品iii级分类名称
            "thirdkindname": Mdesignprocedures.thirdkindname,
            //工时总成本
            "costpricesum": xiaoji,
            //变更标志
            "changetag": "B002-0",
            //工序物料设计标志
            "designmoduletag": "G002-0",
            // 工序物料变更标志
            "designmodulechangetag": "G003-0",
            /*产品名称*/
            "productname": Mdesignprocedures.productname,
            /*产品编号*/
            "productid": Mdesignprocedures.productid,
            /*设计人*/
            "designer": $("#sjr").val(),
            /*登记人*/
            "register": $("#name").val(),
            /*登记时间*/
            "registertime": $("#datetime").val(),
            /*设计要求*/
            "proceduredescribe": $("#proceduredescribe").val(),
            /*审核标志*/
            "checktag": "S001-0"
        };
    let ls = [];
    $.post("/Mdesignprocedure.do/AddMdesignprocedure",Mdesignprocedure, function (sdate) {
        console.log(sdate)
        for (let i = 0; i < list.length; i++) {
            let lss = {
                // 父级序号
                "parentid": sdate.id,
                // 工序序号
                "detailsnumber": list[i].id,
                // 工序编号
                "procedureid": list[i].did,
                // 工序名称
                "procedurename": list[i].dname,
                // 工时数
                "labourhouramount": shu[i],
                // 工序描述
                "proceduredescribe": list[i].dmiaoshu,
                // 单位
                "amountunit": Mdesignprocedures.amountunit,
                // 单位工时成本
                "costprice":dj[i] ,
                // 工时成本小计
                "subtotal": shu[i]*dj[i],
                // 登记人
                "register": $("#name").val(),
                // 登记时间
                "registertime": $("#datetime").val(),
                // 当前工序物料标志
                "designmoduletag": "D002-0",
                // 当前工序物料变更标志
                "designmodulechangetag": "D003-0"
            };
            ls[i] = lss;
        }
        let lsss = JSON.stringify(ls);
        $.post("/Mdesignproceduredetails.do/AddMdesignproceduredetails", {lists: lsss}, function (sd) {
            $("#tishi").text("提交成功！")
            $("#tishi").css("color", "lime")
            $("#staticBackdrop").modal("show");
        })
    })
}

function hh() {
    location.href = "../../li_meng/SouSuo.html";
}







